package DomainModel;

import DataAccess.DataAccessFacade;

/**
 *
 * @author Team2
 */
public class User 
{
    private int userId;
    private String username;
    private String firstName;
    private String lastName;
    private String email;
    private Role role;
    
    public User(int pUserId, String pUsername, String pFirstName, String pLastName, String pEmail, Role pRole) {
        this.userId = pUserId; 
        this.username = pUsername;
        this.firstName = pFirstName;
        this.lastName = pLastName;
        this.email = pEmail;
        this.role = pRole;
    }

    public int getUserId() {
        return userId;
    }

    public String getUsername() {
        return username;
    }
    
   public String getFirstName() {
        return this.firstName;
    }
    
    public String getLastName() {
        return this.lastName;
    }

    public String getEmail() {
        return email;
    }
    
    public Role getRole() {
        return role;
    } 

    public void setUsername(String username) {
        this.username = username;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public void setEmail(String email) {
        this.email = email;
    }
    
    public String validateUsername() {
        if (username.length() < 6 || username.length() > 20) {
            return "Invalid username, length must be between 6 and 20";
        }
            
        return "";
    }
    
    public static String validatePassword(String password) {
        if (password.length() < 6 ) {
            return "Invalid password, length must be greater than 6";
        }
        
        if (!password.matches(".*\\d.*")) {
            return "Invalid password, must contain a number";
        }
        
        if (!password.matches(".*[a-z]+.*")) {
            return "Invalid password, must contain a lower case letter";
        }
        
        if (!password.matches(".*[A-Z].*")) {
            return "Invalid password, must contain an upper case letter";
        }
            
        return "";
    }
    
    public String getMissingFields() {
        String missingFields = "";
        if (firstName == null || firstName.isEmpty()) {
            missingFields = "First Name";
        }
        if (lastName == null || lastName.isEmpty()) {
            missingFields += missingFields.isEmpty() ? "Last Name" : ", Last Name";
        }
        if (email == null || email.isEmpty()) {
            missingFields += missingFields.isEmpty() ? "Email" : ", Email";
        }
        if (username == null || username.isEmpty()) {
            missingFields += missingFields.isEmpty() ? "Username" : ", Username";
        }
        
        if (getRole() == null) {
            missingFields += missingFields.isEmpty() ? "Role" : ", Role";
        }
        
        return missingFields;
    }
    
    public boolean enrolledInSection(int sectionId) throws Exception {
        return DataAccessFacade.getInstance().studentEnrolledInSection(userId, sectionId);
    }
}
